package com.slg.android.manyanime.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.slg.android.manyanime.R;
import com.slg.android.manyanime.bean.ChannelInfo;
import com.slg.android.manyanime.utils.DataStore;

import java.util.List;

/**
 * Created by E540 on 2015/3/13.
 */
public class CategoryDetailAdapter extends BaseAdapter {
    private Context context;
    private List<ChannelInfo> data;

    public CategoryDetailAdapter(Context context, List<ChannelInfo> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null){
            convertView = LayoutInflater.from(context)
                    .inflate(R.layout.item_channel_info_list, parent, false);

        }
        CategoryViewHolder holder = (CategoryViewHolder) convertView.getTag();
        if(holder == null){
            holder = new CategoryViewHolder();
            convertView.setTag(holder);
        }
        ViewUtils.inject(holder,convertView);

        ChannelInfo channelInfo = data.get(position);
        BitmapUtils bitmapUtils = DataStore.getBitmapUtils();
        holder.name.setText(channelInfo.getName());
        holder.score.setText(channelInfo.getScore() + "分");
        if ("1".equals(channelInfo.getIsOver())) {
            //已完结
            holder.total.setText("全" + channelInfo.getTotalNum() + "集");
        } else {
            holder.total.setText("更新至" + channelInfo.getCurNum() + "集");
        }
        bitmapUtils.configDefaultLoadingImage(R.drawable.item_default);
        bitmapUtils.display(holder.pic, channelInfo.getCover());


        return convertView;
    }

    public static class CategoryViewHolder{
        @ViewInject(R.id.item_channel_info_pic)
        public ImageView pic;
        @ViewInject(R.id.item_channel_info_name)
        public TextView name;
        @ViewInject(R.id.item_channel_info_total)
        public TextView total;
        @ViewInject(R.id.item_channel_info_score)
        public TextView score;
    }
}
